import { defineStore } from 'pinia'
import type { useTableType } from '../interface/useTableType'
import { dshList, dshDataHeader } from '@/data/tableData/tableType'
import { tabsTableDsh } from '@/data/tableData/tableData'
// tabs表格接口
import { reqTableData } from '@/api/tableApi/tableApi'
import type { ResApiTableListDetail, ResApiTableList, TableData } from '@/api/tableApi/tableApiType'
const useTable = defineStore('useTable', {
  state: () => ({
    orderStatus: 'dsh',
    // 表格数据

    dshList: [] as any,
    //重置表单
    resetData: [] as any,
    // 表格头部查询
    tableHeader: [] as any,
    // 回收站表格的数据

    // 每条数据
    rowList: {} as any,
    // id的集合
    ids: [] as any,
    // 需要打印的数据
    printingList: [],
    // ------------------------------
    tableList: [] as ResApiTableList[],
    // 页码
    page: 1 as number,
    // 每页条数
    pagesize: 10 as number,
    // 总条数
    total: 0 as number
  }),
  actions: {
    // 获取表格表格数据
    async getTableDataList(val: string) {
      const res = await reqTableData(this.page, this.pagesize)
      const arr = res.data.list.filter((item: ResApiTableList) => {
        if (item.ordersStatus === val) {
          return item
        }
      })
      this.tableList = arr
      this.page = res.data.page
      this.pagesize = res.data.pagesize
      this.total = res.data.total
      console.log('🚚...  表格请求回来的数据', res.data)
    },

    // 表格数据
    searchDshList(val: dshList[]) {
      this.dshList = val
    },
    // 重置头部搜索数据
    resetDshList(val: dshList[]) {
      this.resetData = val
    },
    // 表格查询
    tabsTableDshList(val: string) {
      const arr: any = []
      tabsTableDsh.value.data.list.map((item) => {
        if (item.ordersStatus === val) {
          return arr.push(item)
        }
      })
      this.dshList = arr
      this.resetDshList(arr)
    },

    // 表头数据查询并储存
    setTableList(val: dshDataHeader[]) {
      console.log('🌈成功->信息123', val)
      this.tableHeader = val
    },
    // 表格数据的状态
    tableStatusy(val: string) {
      this.orderStatus = val
    },
    // 进行中表格每条数据
    rowListEvery(val: any) {
      this.rowList = val
    },
    // 删除(js逻辑删除,其实可以发送接口更简单)
    tabsTableDel(val: number) {
      console.log('🌈成功->valnumber', val)
      this.dshList = this.dshList.filter((item: any) => item.id !== val)
    },
    // 批量删除(js逻辑删除,其实可以发送接口更简单)
    tabsTableDelAll() {
      this.ids.map((ids: number) => {
        return (this.dshList = this.dshList.filter((item: any) => item.id !== ids))
      })
    }
  },
  getters: {},
  // 持久化
  persist: {
    enabled: true,
    strategies: [
      {
        // 测试表头数据
        key: 'tableHeader', // 存储的数据Key
        // storage: sessionStorage, //自定义存储到 sessionStorage 和 localStorage
        paths: ['tableHeader'] // 存储的数据
      }
      // ....
    ]
  }
})

export default useTable
